package com.stars.home.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.core.net.toUri
import com.luck.picture.lib.PictureSelector
import com.stars.home.base.MBActivity
import com.stars.home.config.MIntentAction
import com.stars.home.databinding.ActivityRechargeBinding
import com.stars.home.inte.OnUploadCallback
import com.stars.home.model.bean.UpImgBean
import com.stars.home.utils.FileUtils
import com.stars.home.utils.OssUtils
import com.stars.home.vmodel.RechargeVModel
import java.util.ArrayList

/**
 *
 *on 2020/9/24
 */
class RechargeActivity: MBActivity<RechargeVModel, ActivityRechargeBinding>(), OnUploadCallback<UpImgBean> {


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        vm.getBankInfo()

    }


    override fun binding(): ActivityRechargeBinding {
        return ActivityRechargeBinding.inflate(layoutInflater)
    }


    override fun vmClass(): Class<RechargeVModel> {
        return RechargeVModel::class.java
    }

    override fun title(): String? {
        return "充值"
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        if (requestCode == MIntentAction.REQUEST_CODE_THREE) {//图片

            val selectList = PictureSelector.obtainMultipleResult(data)
            val list = ArrayList<String>()
            if (null != selectList) {
                if(selectList[0].path.contains("content://")){

                    val cr = this.contentResolver
                    list.add( FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(),cr))

                }else{
                    list.add(selectList[0].path)
                }

                OssUtils(this, vm)
                        .setUpLoad("recharge", list, this)

            }

            return

        }
        if (requestCode == MIntentAction.REQUEST_CODE_CAMERA) {//拍照
            //拍照
            val selectList = PictureSelector.obtainMultipleResult(data) ?: return
            val list = ArrayList<String>()
            if(selectList[0].path.contains("content://")){

                val cr = this.contentResolver
                list.add( FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(),cr))

            }else{
                list.add(selectList[0].path)
            }
            OssUtils(this, vm)
                    .setUpLoad("recharge", list, this)

            return
        }

    }


    override fun onUploadSuccess(t: UpImgBean) {
//        val ss: Array<String> = t.split(",".toRegex()).toTypedArray()
//        vm.url.value = ss[1]

    }

    override fun onUploadError(error: String?) {
        TODO("Not yet implemented")
    }
}
